const newRules = [
  {
    "id": 1,
    "priority": 1,
    "action": {
      "type": "modifyHeaders",
      "responseHeaders": [
        { "header": "Content-Type", "operation": "append", "value": "application/json; charset=UTF-8" }
      ]
    },
    "condition": {
      "urlFilter": "defaultDispatcher",
      "resourceTypes": ["main_frame", "sub_frame", "script", "xmlhttprequest"]
    }
  }
];

async function addHeader() {
  const oldRules = await chrome.declarativeNetRequest.getDynamicRules();
  const oldRuleIds = oldRules.map(rule => rule.id);
  const add = [];

  // Check if the rule already exists
  for (const rule of newRules) {
    if (!oldRuleIds.includes(rule.id)) {
      add.push(rule);
    }
  }

  console.log(666, add.length);
  if(add.length === 0) {
    return;
  }

  // Use the arrays to update the dynamic rules
  await chrome.declarativeNetRequest.updateDynamicRules({
    // removeRuleIds: oldRuleIds,
    addRules: newRules
  });
}

addHeader();
